package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.InspectionScheme;
import com.mxpio.erp.common.quality.entity.InspectionSchemeChain;
import com.mxpio.erp.common.quality.service.InspectionSchemeChainService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ItemGroup;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InspectionSchemeChainController", description = "物料质检方案接口")
@RestController
@RequestMapping("/erp/quality/ischain/")
public class InspectionSchemeChainController {
	
	@Autowired
	private InspectionSchemeChainService inspectionSchemeChainService;
	
	@GetMapping("scheme/itempage/{schemeCode}")
	@Operation(summary = "物料质检方案对应物料列表(按方案查物料)", description = "获取物料质检方案对应物料列表(按方案查物料)", method = "GET")
	public Result<Page<Item>> getItemPageFromSchemeCode(@PathVariable(name = "schemeCode", required = true) String schemeCode, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = inspectionSchemeChainService.getItemPageFromSchemeCode(pageAble, schemeCode);
		return Result.OK(page);
	}
	
	@GetMapping("item/page")
	@Operation(summary = "物料质检方案列表(按物料)", description = "获取物料质检方案列表(按物料)", method = "GET")
	public Result<Page<Item>> pageByItem(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = inspectionSchemeChainService.pageByItem(pageAble, criteria);
		return Result.OK(page);
	}

	@RequestMapping("save")
    @Operation(summary = "保存物料质检方案", description = "保存物料质检方案", method = "POST")
    public Result<List<InspectionSchemeChain>> save(@RequestBody List<InspectionSchemeChain> chains) {
		inspectionSchemeChainService.save(chains);
        return Result.OK(chains);
    }
	
	@DeleteMapping("scheme/remove/{schemecode}/{itemcodes}/{datascope}")
    @Operation(summary = "根据itemCodes删除物料质检方案", description = "根据itemCode删除物料质检方案", method = "DELETE")
    public Result<InspectionSchemeChain> removeBySupply(@PathVariable(name = "schemecode", required = true) String schemeCode
    		,@Parameter(description="物料代码，多个单号用英文逗号分隔") @PathVariable(name = "itemcodes", required = true) String itemCodes
			,@Parameter(description="1物料，2物料组") @PathVariable(name = "datascope", required = true) String dataScope) {
        String[] itemCode = itemCodes.split(",");
		inspectionSchemeChainService.removeByScheme(schemeCode, itemCode,dataScope);
        return Result.OK();
    }
	
/*	@DeleteMapping("item/remove/{itemCode}/{schemeCodes}")
    @Operation(summary = "根据pnCodes删除物料质检方案", description = "根据pnCodes删除物料质检方案", method = "DELETE")
    public Result<InspectionSchemeChain> removeByItem(@PathVariable(name = "itemCode", required = true) String itemCode
    		,@Parameter(description="供应商代码，多个单号用英文逗号分隔") @PathVariable(name = "schemeCodes", required = true) String schemeCodes) {
        String[] schemeCode = schemeCodes.split(",");
		inspectionSchemeChainService.removeByItem(itemCode, schemeCode);
        return Result.OK();
    }*/
	

	@GetMapping("without/items/page")
	@Operation(summary = "未匹配物料列表", description = "获取未匹配物料列表", method = "GET")
	public Result<Page<Item>> withoutItemByScheme(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Item> page = inspectionSchemeChainService.withoutItemByScheme(pageAble, criteria);
		return Result.OK(page);
	}
	
	/*@GetMapping("without/schemes/page/{itemCode}")
	@Operation(summary = "未匹配的质检方案列表(按物料)", description = "获取未匹配的供应商列表(按物料)", method = "GET")
	public Result<Page<Supply>> withoutSupplyByItem(@PathVariable(name = "itemCode", required = true) String itemCode,
			Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<Supply> page = supplyChainSerivce.withoutSupplyByItem(itemCode, pageAble, criteria);
		return Result.OK(page);
	}*/


	@GetMapping("scheme/itemgrouppage/{schemeCode}")
	@Operation(summary = "物料质检方案对应物料组列表(按方案查物料组)", description = "获取物料质检方案对应物料组列表(按方案查物料组)", method = "GET")
	public Result<Page<ItemGroup>> getItemGroupPageFromSchemeCode(@PathVariable(name = "schemeCode", required = true) String schemeCode, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<ItemGroup> page = inspectionSchemeChainService.getItemGroupPageFromSchemeCode(pageAble, schemeCode);
		return Result.OK(page);
	}


	@GetMapping("without/itemgroups/page")
	@Operation(summary = "未匹配物料组列表", description = "获取未匹配物料组列表", method = "GET")
	public Result<Page<ItemGroup>> withoutItemGroupByScheme(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<ItemGroup> page = inspectionSchemeChainService.withoutItemGroupByScheme(pageAble, criteria);
		return Result.OK(page);
	}

	@GetMapping("itemgroup/page")
	@Operation(summary = "物料质检方案列表(按物料组查方案)", description = "获取物料质检方案列表(按物料组查方案)", method = "GET")
	public Result<Page<ItemGroup>> pageByItemGroup(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<ItemGroup> page = inspectionSchemeChainService.pageByItemGroup(pageAble, criteria);
		return Result.OK(page);
	}



	@GetMapping("getschemelistbypath/{code}/{datascope}")
	@Operation(summary = "根据code+datascope反查质检方案", description = "根据code+datascope反查质检方案", method = "GET")
	public Result<List<InspectionScheme>> getSchemeListByPath(@Parameter(description="物料/组 编码") @PathVariable(name = "code", required = true) String code
			, @Parameter(description="1物料，2物料组") @PathVariable(name = "datascope", required = true) String dataScope) {
		return inspectionSchemeChainService.getSchemeListByPath(code,dataScope);
	}
}
